package tt.langchain4j.service;

import dev.langchain4j.data.message.*;

import java.nio.file.Path;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

public class Requester implements ServiceRequest {

    private final Builder context;

    public Requester(Builder context ) {
        this.context =context;
    }

    public static Builder builder() {
        return new Builder();
    }

    @Override
    public List<Content> getMedias() {
        return context.medias;
    }

    @Override
    public Object getMemoryId() {
        return context.memoryId;
    }

    @Override
    public UserMessage getUserMessage() {
        if ( context.userMessage.isEmpty()) {
            return null;
        }
        return UserMessage.from(context.userMessage.toString());
    }

    @Override
    public SystemMessage getSystemMessage() {
        if ( context.systemMessage.isEmpty()) {
            return null;
        }
        return SystemMessage.from(context.systemMessage.toString());
    }


    public static class Builder {

        private final List<Content> medias = new ArrayList<>();

        private final StringBuffer userMessage = new StringBuffer();

        private final StringBuffer systemMessage = new StringBuffer();

        private Object memoryId;

        public Builder memoryId(String memoryId) {
            this.memoryId = memoryId;
            return this;
        }

        private Builder systemMessage ( String systemMessage ) {
            this.systemMessage.append(systemMessage);
            return this;
        }

        public Builder systemMessage( String pattern , Object... args ) {
            return systemMessage(MessageFormat.format(pattern,args));
        }

        public Builder userMessage( String userMessage ) {
            this.userMessage.append(userMessage);
            return this;
        }

        public Builder userMessage( String pattern , Object... args ) {
            return userMessage(MessageFormat.format(pattern,args));
        }

        public Builder image( Path path ) {
            this.medias.add(ImageContent.from(path.toUri()));
            return this;
        }

        public Builder pdf( Path path ) {
            this.medias.add(PdfFileContent.from(path.toUri()));
            return this;
        }

        public ServiceRequest build() {
            return new Requester(this);
        }



    }







}
